/**
 * Created by Administrator on 2020/11/24.
 */
layui.use(['table','form','layer','layedit', 'laydate'], function(){
    var table = layui.table
        ,form = layui.form
        ,layer = layui.layer
        ,layedit = layui.layedit
        ,laydate = layui.laydate
        ,$=layui.$;


    //监听提交
    form.on('submit(demo1)', function(data){
        table.reload('test',{
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where:data.field
        });
        return false;
    });

    table.render({
        elem: '#test'
        ,url:config.gatewayUri+"/"+config.goodsServiceId+'/api/goods-category/admin/getBackGoodsCategoryList'
        ,cols: [[

            {field:'goodscategoryname', title: '分类名称',width:'40%'}
            ,{field:'cname', title: '创建人',width:'40%'}
            ,{title: '操作', width:'20%', templet:'#newsListBar',fixed:"right",align:"center"}
        ]]
        ,page:true
        ,limits : [5,10]
        ,limit:2
        ,response:{
            statusCode:'200'
        }
        ,parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": res.data.list //解析数据列表
            };
        }
    });
    $("#add").click(function(){
        layer.confirm('*分类名称<input type="text" class="categoryName"/>', { title:'新增商品分类'}, function(index){
            var categoryName=$(".categoryName").val();
            $.ajax({
                url:config.gatewayUri+"/"+config.goodsServiceId+"/api/goods-category/admin/saveGoodsCategory",
                data:"goodscategoryname="+categoryName,
                type:"post",
                dataType:"json",
                success:function(result){
                    if(result.code=='200'){
                        layer.alert("添加成功！");
                        //layer.close(index);
                       table.reload('test',{});
                    }else{
                        layer.alert("添加失败！");
                    }
                }
            });
        });
    });
    table.on('tool(newsList)', function(obj) {
        var layEvent = obj.event,
            data = obj.data;
        if (layEvent === 'edit') { //编辑
            var name=data.goodscategoryname;
            layer.confirm('*分类名称<input type="text" class="categoryName" value='+name+' />', { title:'编辑商品分类'}, function(index){
                var categoryName=$(".categoryName").val();
                $.ajax({
                    url:config.gatewayUri+"/"+config.goodsServiceId+"/api/goods-category/admin/UpdateGoodsCategory",
                    data:{
                        goodscategoryname:categoryName,
                        goodscategoryid:data.goodscategoryid
                    },
                    type:"post",
                    dataType:"json",
                    success:function(result){
                        if(result.code=='200'){
                            layer.alert("修改成功！");
                            table.reload('test',{});
                        }else{
                            layer.alert("修改失败！");
                        }
                    }
                });
            });
        }else if(layEvent=='del'){
            layer.confirm('确定删除'+data.goodscategoryname+"吗？", { title:'编辑商品分类'}, function(index){
                var categoryName=$(".categoryName").val();
                $.ajax({
                    url:config.gatewayUri+"/"+config.goodsServiceId+"/api/goods-category/admin/delGoodsCategory",
                    data:"goodsCategoryId="+data.goodscategoryid,
                    type:"get",
                    dataType:"json",
                    success:function(result){
                        if(result.code=='200'){
                            layer.alert("删除成功！");
                            table.reload('test',{});
                        }else{
                            layer.alert("删除失败！");
                        }
                    }
                });
            });
        }
    });
});